package com.elf.zero.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.elf.zero.app.R;

/**
 * 进度控件
 * Created by Lidong on 2017/11/23.
 */
public class ProgressWidget extends View {

    private Paint paint = new Paint();
    private int mProgressValue = 3;
    private int mMaxValue = 100;
    private int mCenterX = 0;
    private int mCenterY = 0;
    private int mRadius = 0;
    private int mBorderColor;  //边框颜色
    private int mFillColor;   //填充颜色
    private int mProgressColor; //进度填充颜色

    private RectF mOval = new RectF();

    private int mLoopAngle = 270;

    public ProgressWidget(Context context) {
        this(context, null);
    }

    public ProgressWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs){
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ProgressWidget);
            mBorderColor = typedArray.getColor(R.styleable.ProgressWidget_border_color, Color.WHITE);
            mFillColor = typedArray.getColor(R.styleable.ProgressWidget_fill_color, 0x10000000);
            mProgressColor = typedArray.getColor(R.styleable.ProgressWidget_progress_color, 0xeeFFFFFF);
            typedArray.recycle();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
        int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();

        mCenterX = width / 2 - 1;
        mCenterY = height / 2 - 1;

        mCenterX += getPaddingRight();
        mCenterY += getPaddingTop();

        if (height < width) {
            mRadius = width / 2 - 2;
        } else {
            mRadius = height / 2 - 2;
        }

        mOval.set(mCenterX - mRadius + 4, mCenterY - mRadius + 4, mCenterX + mRadius - 4, mCenterY + mRadius - 4);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (getVisibility() != VISIBLE) {
            canvas.drawColor(Color.TRANSPARENT);
            return;
        }

        paint.setAntiAlias(true);

        //画圆环
        paint.setColor(mFillColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(mCenterX, mCenterY, mRadius, paint);

        //画圆环边框
        paint.setColor(mBorderColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2f);
        canvas.drawCircle(mCenterX, mCenterY, mRadius, paint);

        //画进度
        paint.setColor(mProgressColor);

        paint.setStyle(Paint.Style.FILL);
        if (mMaxValue > 0) {
            canvas.drawArc(mOval, 270, 360f * mProgressValue / mMaxValue, true, paint);  //根据进度画圆弧
        } else {
            canvas.drawArc(mOval, mLoopAngle, 90, true, paint);
            mLoopAngle += 3;
            if (mLoopAngle > 360) {
                mLoopAngle = 0;
            }
            if (getVisibility() == VISIBLE) {
                postInvalidate();
            }
        }
    }

    public void setProgress(int value) {
        mProgressValue = value;
        postInvalidate();
    }

    public int getProgress(){
        return mProgressValue;
    }

    public void setMaxValue(int value) {
        mMaxValue = value;
        postInvalidate();
    }

//    public void setColor(int color) {
//        mColor = color;
//        postInvalidate();
//    }
}
